layui.define(['table', 'form','table','element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,table = layui.table, element = layui.element;

    $('#userId').select2({
        placeholder: "请选择用户信息",
        width: '250px',
        theme: 'bootstrap',
        allowClear : true,
        ajax: {
            url: layui.setter.webRoot + '/user/select.data',
            method: 'post',
            delay: 250,
            processResults: function (result) {
                if (result.code == 200) {
                    return {
                        pagination: {more: result.body.more},
                        results: result.body.data || []
                    };
                } else {
                    return {results: []};
                }
            }
        }
    });
	
	$('#shopUserId').select2({
        placeholder: "请选择用户信息",
        width: '250px',
        theme: 'bootstrap',
        allowClear : true,
        ajax: {
            url: layui.setter.webRoot + '/user/select.data',
            method: 'post',
            delay: 250,
            processResults: function (result) {
                if (result.code == 200) {
                    return {
                        pagination: {more: result.body.more},
                        results: result.body.data || []
                    };
                } else {
                    return {results: []};
                }
            }
        }
    });

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/fee_template/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'id', title: '模版ID'}
      ,{field: 'title', title: '模版标题'}
      ,{field: 'userName', title: "所属用户",templet:function (record){
          return record.userName + "(ID:"+record.userId+")";
              }}
      ,{field: 'feeInfo', title: '收费描述'}
      ,{field: 'updateDate', title: '最后修改'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  table.render({
      elem: '#LAY-table-shop'
      ,url: layui.setter.webRoot + '/bxscn/shop_fee_template/list.data'
      ,method: 'post'
      ,parseData: function (res) {
          return {
              "code": res.code,
              "msg": res.msg,
              "count": res.body.total,
              "data": res.body.data
          };
      }
      ,response: {statusCode: 200}
      ,cols: [[
        {field: 'id', title: '模版ID'}
        ,{field: 'title', title: '模版标题'}
        ,{field: 'userName', title: "所属用户",templet:function (record){
            return record.userName + "(ID:"+record.userId+")";
                }}
        ,{field: 'itemCount', title: '收费项数量'}
        ,{field: 'updateDate', title: '最后修改'}
        ,{field: 'createDate', title: '创建时间'}
        ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#LAY-table-shop-toolbar'}
      ]]
      ,page: true
      ,limit: 30
      ,height: 'full-220'
      ,text: '对不起，加载出现异常！'
    });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //监听搜索
  form.on('submit(LAY-search-shop)', function(data){
      var field = data.field;
      //执行重载
      table.reload('LAY-table-shop', {
        where: field
        ,page: {curr: 1}
      });
  });
  
  element.on('tab(tab-info)', function (data) {
      if(data.index == 0) {
          table.reload('LAY-table')
      }else if(data.index == 1) {
          table.reload('LAY-table-shop')
      }
  });
  
  //事件
  var active = {
      add: function(){
          layer.open({
              type: 2
              ,title: '新增便携式储能电池收费模版'
              ,content: layui.setter.webRoot + '/bxscn/fee_template/add.html'
              ,maxmin: false
              ,area: ['500px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/fee_template/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,update: function(obj){
          layer.open({
              type: 2
              ,title: '修改便携式储能电池收费模版'
              ,content: layui.setter.webRoot + '/bxscn/fee_template/update.html?id=' + obj.data.id
              ,maxmin: false
              ,area: ['500px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/fee_template/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,del:function(obj){
    	layer.confirm('确定要删除该收费模版吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/bxscn/fee_template/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
	,addShop: function(){
	      layer.open({
	          type: 2
	          ,title: '新增便携式储能电池收费模版'
	          ,content: layui.setter.webRoot + '/bxscn/shop_fee_template/add.html'
	          ,maxmin: false
	          ,area: ['500px', '500px']
	          ,btn: ['确定', '取消']
	          ,yes: function(index, layero){
	              var iframeWindow = window['layui-layer-iframe'+ index]
	                  ,submitID = 'LAY-form-submit'
	                  ,submit = layero.find('iframe').contents().find('#'+ submitID);
	              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
	                  var field = data.field;
	                  $.ajax({
	                      type: "post",
	                      url: layui.setter.webRoot + '/bxscn/shop_fee_template/add.do',
	                      data:field,
	                      dataType:"json",
	                      success:function(res){
	                          if(res.code == 200){
	                              table.reload('LAY-table-shop');
	                              layer.close(index);
	                          }else{
	                              layer.msg(res.msg,{icon: 2});
	                          }
	                      }
	                  });
	              });
	              submit.trigger('click');
	          }
	      });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('bxscn_fee_template_list', {})
});